
/*
 * JDCompare.java
 *
 * Created on Apr 6, 2011, 11:51:19 AM
 */
import java.text.DecimalFormat;
import java.util.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author CNTT
 */
public class JDCompare extends javax.swing.JDialog {

    /** Creates new form JDCompare */
    public JDCompare(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        jTable1.getSelectionModel().addListSelectionListener(new RowColumnListSelectionListener1());
        jTable2.getSelectionModel().addListSelectionListener(new RowColumnListSelectionListener2());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        lb = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lbNgay1 = new javax.swing.JLabel();
        lbKhuVuc = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lbNgay2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        lb.setText("Khu Vực:");

        jLabel2.setText("Ngày:");

        lbNgay1.setText("jLabel2");

        lbKhuVuc.setText("jLabel1");

        jLabel3.setText("Ngày:");

        lbNgay2.setText("jLabel3");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lb)
                        .addGap(18, 18, 18)
                        .addComponent(lbKhuVuc))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(27, 27, 27)
                        .addComponent(lbNgay1)
                        .addGap(346, 346, 346)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(lbNgay2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(249, 249, 249))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(lbNgay2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lb, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbKhuVuc))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbNgay1)
                            .addComponent(jLabel2))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 955, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 493, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                JDCompare dialog = new JDCompare(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    //Xu Ly Su Kien Chon Hang Trong JTable1 Va JTable2
    private class RowColumnListSelectionListener1 implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (jTable1.getRowSelectionAllowed()) {
                jTable2.setRowSelectionInterval(jTable1.getSelectedRow(), jTable1.getSelectedRow());
            }
        }
    }

    private class RowColumnListSelectionListener2 implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (jTable2.getRowSelectionAllowed()) {
                jTable1.setRowSelectionInterval(jTable2.getSelectedRow(), jTable2.getSelectedRow());
            }
        }
    }

    public void compareGiaVang(String khuvuc, String ngay1, String ngay2) {
        lb.setText("Khu Vực");
        lbKhuVuc.setText(khuvuc);
        lbNgay1.setText(ngay1);
        lbNgay2.setText(ngay2);
        try {
            UDPCC udp = new UDPCC();
            ArrayList a = new ArrayList();
            ArrayList b = new ArrayList();
            a = udp.ketNoi(Constants.GIAVANG + "," + khuvuc + "," + ngay1);
            b = udp.ketNoi(Constants.GIAVANG + "," + khuvuc + "," + ngay2);
            Object[][] obj1 = new Object[a.size() / 4][4];
            int i, j;
            for (i = 0; i < a.size() / 4; ++i) {
                for (j = 0; j < 3; ++j) {
                    obj1[i][j] = a.get(i * 4 + j + 1);
                }
            }

            Object[][] obj2 = new Object[b.size() / 4][4];
            for (i = 0; i < b.size() / 4; ++i) {
                for (j = 0; j < 3; ++j) {
                    obj2[i][j] = b.get(i * 4 + j + 1);
                }
            }
            String tmp1, tmp2;
            for (i = 0; i < obj1.length; ++i) {
                tmp1 = change(obj1[i][1], obj2[i][1]);
                tmp2 = change(obj2[i][1], obj1[i][1]);
                obj1[i][3] = (Object) tmp1;
                obj2[i][3] = (Object) tmp2;
            }

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                    obj1,
                    new String[]{
                        "Loại Vàng", "Giá Bán", "Giá Mua", "Tăng/Giảm"
                    }));
            jTable2.setModel(new javax.swing.table.DefaultTableModel(
                    obj2,
                    new String[]{
                        "Loại Vàng", "Giá Bán", "Giá Mua", "Tăng/Giảm"
                    }));

        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    public void compareTiGia(String tenNT, String ngay1, String ngay2) {
        lb.setText("Tên NT");
        lbKhuVuc.setText(tenNT);
        lbNgay1.setText(ngay1);
        lbNgay2.setText(ngay2);
        try {
            UDPCC udp = new UDPCC();
            ArrayList a = new ArrayList();
            ArrayList b = new ArrayList();
            a = udp.ketNoi(Constants.TIGIA + "," + ngay1 + "," + tenNT);
            b = udp.ketNoi(Constants.TIGIA + "," + ngay2 + "," + tenNT);
            int i, j;
            Object[][] obj1 = new Object[a.size() / 4][4];
            for (i = 0; i < a.size() / 4; ++i) {
                for (j = 0; j < 4; ++j) {
                    obj1[i][j] = a.get(i * 4 + j);
                }
            }

            Object[][] obj2 = new Object[b.size() / 4][4];
            for (i = 0; i < b.size() / 4; ++i) {
                for (j = 0; j < 4; ++j) {
                    obj2[i][j] = b.get(i * 4 + j);
                }
            }
            String tmp1, tmp2;
            System.out.println(a.size() + "VS" + b.size());
            for (i = 0; i < a.size() / 4; ++i) {
                for (j = 1; j < 4; ++j) {
                    // Object temp=obj1[i][j];
                    tmp1 = changeNT(obj1[i][j], obj2[i][j]);
                    tmp2 = changeNT(obj2[i][j], obj1[i][j]);
                    obj1[i][j] = (Object) (obj1[i][j].toString() + tmp1);
                    obj2[i][j] = (Object) obj2[i][j].toString() + tmp2;
                }
            }

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                    obj1,
                    new String[]{
                        "Tên NT", "Mua Tiền Mặt", "Mua Chuyển Khoản", "Bán"
                    }));
            jTable2.setModel(new javax.swing.table.DefaultTableModel(
                    obj2,
                    new String[]{
                        "Tên NT", "Mua Tiền Mặt", "Mua Chuyển Khoản", "Bán"
                    }));

        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    public String change(Object a, Object b) throws Exception {

        float t1, t2, kq;
        t1 = Float.parseFloat(a.toString());
        t2 = Float.parseFloat(b.toString());
        kq = t1 - t2;
        DecimalFormat df = new DecimalFormat("0.000");
        String kq1 = " +" + df.format(kq);
        String kq2 = " -" + df.format(Math.abs(kq));
        if (kq < 0) {
            return kq2;
        }
        return kq1;

    }

    public String changeNT(Object a, Object b) throws Exception {
        StringBuilder s1, s2;
        if (a.equals("-") || b.equals("-")) {
            return "-";
        }
        s1 = new StringBuilder(a.toString());
        s2 = new StringBuilder(b.toString());
        if (s1.lastIndexOf(",") > 0) {
            s1.deleteCharAt(s1.lastIndexOf(","));
        }
        if (s2.lastIndexOf(",") > 0) {
            s2.deleteCharAt(s2.lastIndexOf(","));
        }
        Float tmp1 = Float.parseFloat(s1.toString());
        Float tmp2 = Float.parseFloat(s2.toString());
        Float kq = tmp1 - tmp2;
        DecimalFormat df = new DecimalFormat("0.00");
        String kq1 = " +" + df.format(kq);
        String kq2 = " -" + df.format(Math.abs(kq));
        if (kq < 0) {
            return kq2;
        }
        return kq1;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JLabel lb;
    private javax.swing.JLabel lbKhuVuc;
    private javax.swing.JLabel lbNgay1;
    private javax.swing.JLabel lbNgay2;
    // End of variables declaration//GEN-END:variables
}
